<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;

/**
 * 用户消息通知管理
 * Class Address
 * @package app\data\controller\api\auth
 */
class Message extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'BaseUserMessage';

    /**
     * 获取消息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $data = $this->_vali([
            'type.default'    => 'system',
        ]);
        if ($data['type'] == 'system') {
            $where[] = ['type', '=', 'system'];

            if ($this->user['service_provider_status'] === 1) {
                $where[] = ['send_type', 'in', ['all', 'service_provider', '']];
            } else {
                $where[] = ['send_type', 'in', ['all', 'user', '']];
            }
            $where[] = ['uuid', '=', 0];
        } else {
            $where[] = ['uuid', '=', $this->uuid];
            $where[] = ['type', '=', 'order'];
        }

        $where[] = ['deleted', '=', 0];
        $where[] = ['status', '=', 1];

        $query = $this->_query($this->table)->field('id,content,check_at,read_uuids');
        $query->equal('id')->where($where)->whereOr(function ($query2) use ($data) {
            return $query2->where('type', $data['type'])->where('uuid', $this->uuid);
        });
        $result = $query->order('check_at desc')->page(true, false, false, input('limit', 15));

        foreach ($result['list'] as &$item) {
            if (is_null($item['read_uuids'])) {
                $item['read_uuids'] = [];
            } else {
                $item['read_uuids'] = json_decode($item['read_uuids'], true);
            }

            if (in_array($this->uuid, $item['read_uuids'])) {
                $item['is_read'] = 1;
            } else {
                $item['is_read'] = 0;
            }
            unset($item['read_uuids']);
        }

        $this->success('获取消息列表！', $result);
    }

    /**
     * 消息详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        $result = $this->app->db->name($this->table)->where('id', input('id'))->findOrEmpty();

        if ($result) {
            if (is_null($result['read_uuids'])) {
                $result['read_uuids'] = [];
            } else {
                $result['read_uuids'] = json_decode($result['read_uuids'], true);
            }

            if (!in_array($this->uuid, $result['read_uuids'])) {
                $result['read_uuids'] = array_merge([$this->uuid], $result['read_uuids']);
                $this->app->db->name($this->table)->where('id', input('id'))->update(['read_uuids' => json_encode($result['read_uuids'])]);
            }
        }

        $this->success('获取消息详情！', $result);
    }

    /**
     * 未读消息总览
     * @return int[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function overview()
    {
        $result = $this->app->db->name($this->table)->where('uuid', $this->uuid)->whereOr('uuid', 0)->select();
        foreach ($result as $item) {
            if (is_null($item['read_uuids'])) {
                $item['read_uuids'] = [];
            } else {
                $item['read_uuids'] = json_decode($item['read_uuids'], true);
            }

            if (!in_array($this->uuid, $item['read_uuids'])) {
                $this->success('未读消息总览', ['has_no_read' => 1]);
            }
        }

        $this->success('未读消息总览', ['has_no_read' => 0]);
    }
}